package com.cjd.view.radar

import android.content.Context
import android.graphics.Color
import android.text.TextUtils
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import com.cjd.view.RippleView
import java.util.*


/**
 * @Author chenjidong
 * @email 374122600@qq.com
 * created 2019/9/23
 * description 带波纹文字 在指定区域内 根据矩阵显示数量
 * */
class RadarMatrixTextView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null,
    defStyleAttr: Int = 0
) : FrameLayout(context, attributeSet, defStyleAttr) {

    private var max = 0

    private var rippleW = 0
    private var rippleH = 0
    var hSize = 0
    var wSize = 0

    private val random: Random by lazy {
        Random()
    }

    private var mWidth: Int = 0
    private var mHeight: Int = 0
    private val TEXT_SIZE = 8

    private val keywords = mutableMapOf<Int, String>()

    private var mode = RippleView.MODE_OUT
    //字体颜色
    private val fontColor = Color.parseColor("#ffffff")
    //阴影
    private val shadowColor = Color.parseColor("#999999")
    //波纹颜色
    var rippleColor = Color.parseColor("#ff0000")

    val ignoreIndexArray = arrayListOf<Int>()

    private var onRippleOutViewClickListener: ((view: View) -> Unit)? = null

    fun setOnRippleViewClickListener(listener: (view: View) -> Unit) {
        onRippleOutViewClickListener = listener
    }


    fun dp2px(dp: Float): Int {
        val scale = context.resources.displayMetrics.density
        return (dp * scale + 0.5f).toInt()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val specWidth = MeasureSpec.getSize(widthMeasureSpec)
        val specHeight = MeasureSpec.getSize(heightMeasureSpec)

        if (MeasureSpec.EXACTLY == MeasureSpec.getMode(widthMeasureSpec)) {
            mWidth = specWidth
        } else if (MeasureSpec.AT_MOST == MeasureSpec.getMode(widthMeasureSpec)) {
            mWidth = specWidth - (paddingLeft + paddingRight)
        } else
            mWidth = dp2px(200F)

        if (MeasureSpec.EXACTLY == MeasureSpec.getMode(heightMeasureSpec)) {
            mHeight = specHeight
        } else if (MeasureSpec.AT_MOST == MeasureSpec.getMode(heightMeasureSpec)) {
            mHeight = specHeight - (paddingTop + paddingBottom)
        } else
            mHeight = dp2px(200F)

        setMeasuredDimension(mWidth, mHeight)

        rippleH = dp2px(50F)
        rippleW = dp2px(50F)

        hSize = mHeight / rippleH
        wSize = mWidth / rippleW

        max = hSize * wSize

        ignoreIndexArray.clear()
        ignoreIndexArray.add(0)
        ignoreIndexArray.add(wSize - 1)
        ignoreIndexArray.add((wSize * (hSize - 1)) - 1)
        ignoreIndexArray.add(wSize * hSize - 1)

    }

    fun show() {
        this.removeAllViews()

        for (i in 0 until max) {
            val keyword = "$i"
            addKeyword(keyword)
        }
    }

    private fun addRippleView(i: Int, keyword: String) {

        // 颜色
        val ranColor = fontColor

        val txtSize = TEXT_SIZE
        // 实例化RippleOutView
        val txt = RippleView(context)
        txt.setMode(mode)

        txt.setOnClickListener { view ->
            onRippleOutViewClickListener?.invoke(view)
        }
        txt.text = keyword
        txt.setTextColor(ranColor)
        txt.setRippleColor(rippleColor)
        txt.setTextSize(TypedValue.COMPLEX_UNIT_SP, txtSize.toFloat())
        txt.setShadowLayer(1F, 1F, 1F, shadowColor)
        txt.gravity = Gravity.CENTER

        txt.startRippleAnimation()

        val layParams = LayoutParams(rippleW, rippleH)
        val index = i % wSize
        val leftMargin = index * rippleW

        layParams.leftMargin = leftMargin
        layParams.topMargin = i / wSize * rippleH
        addView(txt, layParams)
    }

    /**
     * 添加RippleOutView的内容
     *
     * @param keyword
     */
    fun addKeyword(keyword: String) {
        if (keywords.size < max) {
            if (!keywords.containsValue(keyword)) {
                val key = getExcludeIgnoreRandom()
                if (TextUtils.isEmpty(keywords[key])) {
                    keywords[key] = keyword
                    addRippleView(key, keyword)
                }
            }
        }
    }

    private fun getExcludeIgnoreRandom(): Int {
        val key = random.nextInt(max)
        if (ignoreIndexArray.contains(key)) {
            return getExcludeIgnoreRandom()
        }
        return key
    }
}